//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.crop.dao.OrderDao;
import com.qst.crop.dao.UserDao;
import com.qst.crop.entity.Order;
import com.qst.crop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Iterator;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    private Integer pageSize = 30;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private UserDao userDao;

    public OrderServiceImpl() {
    }

    public PageInfo<Order> selectAll(Integer pageNum) {
        PageHelper.startPage(pageNum, this.pageSize);
        List<Order> goods = this.orderDao.selectAll();
        PageInfo<Order> orderPageInfo = new PageInfo(goods);
        return orderPageInfo;
    }

    public PageInfo<Order> selectAllGoods(Integer pageNum) {
        Order order = new Order();
        order.setType("goods");
        PageHelper.startPage(pageNum, this.pageSize);
        List<Order> goods = this.orderDao.selectAllGoods(order);
        PageInfo<Order> orderPageInfo = new PageInfo(goods);
        return orderPageInfo;
    }

    public PageInfo<Order> selectAllNeeds(Integer pageNum) {
        Order order = new Order();
        order.setType("needs");

        try {
            UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            Iterator it = principal.getAuthorities().iterator();

            while(it.hasNext()) {
                Object obj = it.next();
                String role = obj.toString();
                if (!role.equals("admin")) {
                    order.setOrderStatus(0);
                    break;
                }
            }
        } catch (Exception var7) {
        }

        PageHelper.startPage(pageNum, this.pageSize);
        List<Order> orders = this.orderDao.selectAllNeeds(order);
        PageInfo<Order> orderPageInfo = new PageInfo(orders);
        return orderPageInfo;
    }

    public void add(Order order) {
        String address = this.userDao.selectAddressByName(order.getOwnName());
        if (!StringUtils.isEmpty(address)) {
            order.setAddress(address);
        }

        this.orderDao.insertSelective(order);
    }

    public void update(Order order) {
        this.orderDao.updateByPrimaryKeySelective(order);
    }

    public void delete(Integer id) {
        this.orderDao.deleteByPrimaryKey(id);
    }

    public PageInfo<Order> selectByType(String type, Integer pageNum) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Order order = new Order();
        Iterator it = principal.getAuthorities().iterator();

        while(it.hasNext()) {
            Object obj = it.next();
            String role = obj.toString();
            if (!role.equals("admin")) {
                order.setOwnName(name);
                order.setOrderStatus(0);
                break;
            }
        }

        order.setType(type);
        PageHelper.startPage(pageNum, this.pageSize);
        List<Order> orders = this.orderDao.selectByExample(order);
        PageInfo<Order> orderPageInfo = new PageInfo(orders);
        return orderPageInfo;
    }

    public Order selectById(Integer id) {
        Order order = this.orderDao.selectByPrimaryKey(id);
        return order;
    }

    public PageInfo<Order> selectGoodsByKeys(String keys, Integer pageNum, String name) {
        Order order = new Order();
        order.setType("goods");
        order.setContent(keys);

        try {
            UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            System.out.println("=========" + principal);
            Iterator it = principal.getAuthorities().iterator();

            while(it.hasNext()) {
                Object obj = it.next();
                String role = obj.toString();
                if (!role.equals("admin")) {
                    order.setOwnName(name);
                    order.setOrderStatus(0);
                    break;
                }
            }
        } catch (Exception var9) {
        }

        PageHelper.startPage(pageNum, this.pageSize);
        List<Order> orders = this.orderDao.selectByKeys(order);
        System.out.println(orders);
        PageInfo<Order> orderPageInfo = new PageInfo(orders);
        return orderPageInfo;
    }

    public PageInfo<Order> selectNeedsByKeys(String keys, Integer pageNum, String name) {
        Order order = new Order();
        order.setType("needs");
        order.setContent(keys);

        try {
            UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            Iterator it = principal.getAuthorities().iterator();

            while(it.hasNext()) {
                Object obj = it.next();
                String role = obj.toString();
                if (!role.equals("admin")) {
                    order.setOwnName(name);
                    order.setOrderStatus(0);
                    break;
                }
            }
        } catch (Exception var9) {
        }

        PageHelper.startPage(pageNum, this.pageSize);
        List<Order> orders = this.orderDao.selectByKeys(order);
        System.out.println(orders);
        PageInfo<Order> orderPageInfo = new PageInfo(orders);
        return orderPageInfo;
    }

    public PageInfo<Order> selectAllByKeys(String keys, Integer pageNum) {
        Order order = new Order();
        order.setContent(keys);
        PageHelper.startPage(pageNum, this.pageSize);
        List<Order> orders = this.orderDao.selectByKeys(order);
        PageInfo<Order> orderPageInfo = new PageInfo(orders);
        return orderPageInfo;
    }

    public void takeDown(String orderId) {
        this.orderDao.takeDown(orderId);
    }

    public void takeUp(String orderId) {
        this.orderDao.takeUp(orderId);
    }
}
